package com.sgb.kjwl.view.ui.fragment.self;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLandStateBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.self.LeaseLandEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.adapter.self.publish.LandStateAdapter;
import com.sgb.kjwl.viewmodel.self.publish.LandStateModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * 土地
 */
public class LandStateFragment extends AppFragment {
    public static final String TAG = "";
    public static final String KEY_TYPE = "type";
    public static final String OPT_UP = "1";
    public static final String OPT_DOWN = "2";
    public static final String OPT_DEL = "3";

    private FragmentLandStateBinding binding;
    @Inject
    LandStateModel viewModel;
    private String status = "";
    private LandStateAdapter stateAdapter;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            if (viewModel != null) {
                viewModel.getPublishListModel(false);
            }
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static LandStateFragment newInstance(String type) {
        LandStateFragment fragment = new LandStateFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_land_state, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initView();
        initData();
    }

    private void initView() {
        if (stateAdapter == null) {
            stateAdapter = new LandStateAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getResources().getDimension(R.dimen.dp10), getResources().getColor(R.color.color_F5F5F5)));
        binding.rlvView.setAdapter(stateAdapter);
        binding.rlvView.setLoadingMoreEnabled(true);
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getPublishListModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getPublishListModel(true);
            }
        });
        stateAdapter.setItemCallBack(new LandStateAdapter.ItemCallBack() {
            @Override
            public void del(String id) {
                optState(OPT_DEL, id);
            }

            @Override
            public void up(String id) {
                optState(OPT_UP, id);
            }

            @Override
            public void down(String id) {
                optState(OPT_DOWN, id);
            }

            @Override
            public void detail(String id) {
                viewModel.getH5Url(id+"&iosFlag=2");
            }
        });
    }

    private void initData() {
        status = (String) getArguments().get(KEY_TYPE);
        viewModel.map.get().put("status", status);
        viewModel.getPublishListModel(false);
    }

    public void showList(List<LeaseLandEntity> datas, boolean isLoadMore) {
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
            if (datas.size() != 0) {
                stateAdapter.addDatas(datas);
            }
        } else {
            if (datas.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.rlvView.refreshComplete();
            binding.rlvView.setLoadingMoreEnabled(true);
            stateAdapter.setDatas(datas);
        }
    }

    private void optState(String opt, String id) {
        if (opt.equals(OPT_UP)) {
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("上架提示")
                    .setMsg("您确定要上架当前信息？")
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            viewModel.upDownStateModel(opt, id);
                        }

                        @Override
                        public void onLeftClick() {

                        }
                    }).show();
        } else if (opt.equals(OPT_DOWN)) {
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("下架提示")
                    .setMsg("您确定要下架当前信息？")
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            viewModel.upDownStateModel(opt, id);
                        }

                        @Override
                        public void onLeftClick() {

                        }
                    }).show();
        } else if (opt.equals(OPT_DEL)) {
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("删除提示")
                    .setMsg("您确定要删除当前信息？ 删除后，将不可恢复")
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            viewModel.delModel(opt, id);
                        }

                        @Override
                        public void onLeftClick() {

                        }
                    }).show();
        }
    }


    public void showOptSuccess(String opt) {
        String msg = "操作成功";
        if (opt.equals(OPT_UP)) {
            msg = "上架成功";
        } else if (opt.equals(OPT_DOWN)) {
            msg = "下架成功";
        } else if (opt.equals(OPT_DEL)) {
            msg = "删除成功";
        }
        MToast.showToast(getActivity(), msg);
        viewModel.getPublishListModel(false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PUBLISH_LAND_REFUSH) {
            String text = (String) event.getMessageData();
            if (TextUtils.isEmpty(text)) text = "";
            viewModel.map.get().put("title", text);
            viewModel.getPublishListModel(false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
